<?php
namespace App\Http\Query;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Log;

class CategoryQuery extends Model
{

    // 设置表名NAME="category_master";
    // 设置表名
    const TABLE_NAME = "category_master";

    protected $table = 'category_master';

    protected $primaryKey = 'category_id';

    protected $CategoryMaster = self::TABLE_NAME;

    public $timestamps = false;

    public function delCategoryMaster($code) // 删
    {
        $sql = 'update category_master set delete_flg = 1 where category_id=?';
        $param = [
            $code
        ];
        $delflag = DB::update($sql, $param);
        return $delflag;
    }

    public function updCategoryMaster($data, $list, $arr) // 改
    {
        $CategoryMaster = $this->where($list, '=', $data);
        return $CategoryMaster->update($arr);
    }

    public function addCategoryMaster($category) // 增
    {
        if (empty($category["category_id"])) {
            $sql = 'INSERT INTO  category_master (category_code, category_name, category_en, category_image, delete_flg, created_at, updated_at) VALUES (?,?,?,?,0,now(),now())';
            $param = [
                $category["category_code"],
                $category["category_name"],
                $category["category_en"],
                $category["category_image"]
            ];
            DB::insert($sql, $param);
        } else {
            $sql = 'UPDATE category_master  SET  category_code = ? , category_name = ?, category_en = ?, category_image = ?, delete_flg = 0, created_at = now(), updated_at = now() WHERE delete_flg = 0 and category_id = ?';
            $param = [
                $category["category_code"],
                $category["category_name"],
                $category["category_en"],
                $category["category_image"],
                $category["category_id"]
            ];
            DB::update($sql, $param);
        }

        return true;
    }

    public function getCategoryInfoByLoginId()
    {
        $sql = 'select * from category_master where  delete_flg = 0';

        $users = DB::select($sql);
        return $users;
    }

    public function addCsvCategoryMaster($CategoryMaster)
    {

        // Log::info($CategoryMaster);
        if (isset($CategoryMaster[0]) && ! empty($CategoryMaster[0])) {
            $sql = 'INSERT INTO  category_master (category_code, category_name, category_en, category_image, delete_flg, created_at, updated_at) VALUES (?,?,?,?,0,now(),now())';
            $param = $CategoryMaster;
            return DB::insert($sql, $param);
        }
    }
}
